/**
 * Copyright (C) 2015-2018 IQIYI
 * All rights reserved.
 *
 * Author     : 王海斌
 * E-mail     : wanghaibin@qiyi.com
 * Version    :
 * Date       : Tue 09 Oct 2018 06:05:59 PM CST
 * Destription: 测试赋值给enum不存在的值
 *
 */
#include <iostream>
#include <string.h>

using namespace std;

typedef enum ENUM_TEST
{
    ENUM_T1 = 100,
    ENUM_T2 = 200
} ENUM_TEST;

enum REG_ZONE {
    ZONE_NONE = 0,
    ZONE_BEIJING,
    ZONE_SHANGHAI,
    ZONE_MAX,
};

typedef struct REG_ZONE_INFO {
    REG_ZONE zone_;  //区域代码
    const char* str_;
}REG_ZONE_INFO;

static REG_ZONE_INFO zone_info_[] {
    {ZONE_NONE,         "unknow"},
        {ZONE_BEIJING,      "beijing"},
        {ZONE_SHANGHAI,     "shanghai"},
        {ZONE_MAX,          NULL},
};

const char * get_zone_str(int code)
{
    if(code>=ZONE_MAX||code<=ZONE_NONE) {
        return zone_info_[ZONE_NONE].str_;
    } else {
        return zone_info_[code].str_;
    }
}

REG_ZONE get_zone_code(const char * str)
{
    int  nn = ZONE_NONE;
    for (;;) {
        if (zone_info_[nn].str_ == NULL) {
            return ZONE_NONE;
        } else if(0==strcmp(zone_info_[nn].str_, str)) {
            return (REG_ZONE)nn;
        }
        nn++;
    }
}

int main(int , char **)
{
    ENUM_TEST xx = ENUM_T1;
    cout << "xx=" << xx << endl;
    xx = (ENUM_TEST)-100;
    cout << "xx=" << xx << endl;
    cout << "get_zone_str(100)" << get_zone_str(100) << endl;
    cout << "get_zone_str(1)" << get_zone_str(1) << endl;
    cout << "get_zone_code(shanghai)" << get_zone_code("shanghai") << endl;
    cout << "get_zone_code(tianjin)" << get_zone_code("tianjin") << endl;
    return 0;
}
